import requests
from bs4 import BeautifulSoup
class Scrawler():
    def __init__(self):

        self.headers = {
            "User - Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15"
        }
#爬文本
    def getTargetText(self,url, selector):
        codes = requests.get(url, headers=self.headers).text
        bs = BeautifulSoup(codes, 'html.parser')
        return [item.text for item in bs.select(selector=selector)]
#爬图片
    def getTargetImg(self,imgurl):
        imgc = requests.get(imgurl).content
        with open("1.jpg",'wb') as f:
            f.write(imgc)
if __name__ == '__main__':
    url = "https://new.qq.com/rain/a/20230409A00J9Q00"
    s = Scrawler()
    imgurl="https://inews.gtimg.com/news_bt/Ow9lruFYEmSXIT0g0F57TlGY-8npJY7UipZP0EYwldXakAA/1000"
    print(s.getTargetImg(imgurl))
